//
//  ViewController.m
//  05-HomeWork
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenBounds [UIScreen mainScreen].bounds
@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSArray *imageArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    self.imageArray = [NSArray arrayWithObjects:@"07", @"01", @"02", @"03", @"04", @"05", @"06", @"07", @"01", nil];
    for (int i = 0; i < self.imageArray.count; i++) {
        
        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(i * kScreenWidth, 0, kScreenWidth, kScreenHeight)];
        smallScrollView.delegate = self;
        smallScrollView.minimumZoomScale = 0.5;
        smallScrollView.maximumZoomScale = 2;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        imageView.image = [UIImage imageNamed:self.imageArray[i]];
        [smallScrollView addSubview:imageView];
        [self.scrollView addSubview:smallScrollView];
    }
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentOffset = CGPointMake(1 * kScreenWidth, 0);
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(kScreenWidth * self.imageArray.count, kScreenHeight);
    [self.view addSubview:self.scrollView];
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, kScreenHeight -100, kScreenWidth - 20, 50)];
    [self.view addSubview:self.pageControl];
    self.pageControl.numberOfPages = 7;
    self.pageControl.currentPage = 0;
    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageControl.pageIndicatorTintColor = [UIColor cyanColor];
    [self.pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
}

- (void)pageChange:(UIPageControl *)pageControl {
    if (self.scrollView.contentOffset.x == 0) {
        pageControl.currentPage = 6;
    } else if (self.scrollView.contentOffset.x == (self.imageArray.count - 1) * kScreenWidth) {
                pageControl.currentPage =  0;
    } else {
    NSInteger index = pageControl.currentPage;
    self.scrollView.contentOffset = CGPointMake((index + 1) * kScreenWidth, 0);
    }
}
//开始缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    return [scrollView.subviews firstObject];
}
//结束缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x > (self.imageArray.count - 1) * kScreenWidth) {
    self.scrollView.contentOffset = CGPointMake(1 * kScreenWidth , 0) ;
    } else if (scrollView.contentOffset.x < 0) {
    self.scrollView.contentOffset = CGPointMake((self.imageArray.count - 2) * kScreenWidth, 0);
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  {
    if (scrollView == self.scrollView) {
        if (scrollView.contentOffset.x == 0) {
        self.pageControl.currentPage = 6;
    UIScrollView *smallScrollView = [self.scrollView.subviews objectAtIndex:self.imageArray.count - 1];
            smallScrollView.zoomScale = 1;
            
    } else if (scrollView.contentOffset.x == (self.imageArray.count - 1) * kScreenWidth) {
        self.pageControl.currentPage = 0;
        UIScrollView *smallScrollView = [self.scrollView.subviews objectAtIndex:self.imageArray.count - 2];
        smallScrollView.zoomScale = 1;
    } else {
        NSInteger i = scrollView.contentOffset.x / kScreenWidth;
        self.pageControl.currentPage = i - 1;
        UIScrollView *smallScrollView = [self.scrollView.subviews objectAtIndex:i - 1];
        smallScrollView.zoomScale = 1;
    }
    } else {
        NSLog(@"小scrollView");
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
